﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _1103SimplePainter
{
    public partial class Form1 : Form
    {
        ImageManClass imc = new ImageManClass(500, 500);
        ControlClass cc = new ControlClass();

        public Form1()
        {
            InitializeComponent();
            //初始化PictureBox控件，
            pictureBox1.BorderStyle = BorderStyle.FixedSingle;
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
            //令pictureBox的Image属性为内存中的图片对象
            pictureBox1.Image = imc.backBMP;
            pictureBox1.Width = imc.backBMP.Width;
            pictureBox1.Height = imc.backBMP.Height;
            //初始化各下拉框的下拉列表
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.Items.Add("1像素");
            comboBox1.Items.Add("5像素");
            comboBox1.Items.Add("10像素");
            comboBox1.SelectedIndex = 0;
            comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox2.Items.Add("红色");
            comboBox2.Items.Add("绿色");
            comboBox2.Items.Add("蓝色");
            comboBox2.SelectedIndex = 0;
            comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox3.Items.Add("红色");
            comboBox3.Items.Add("绿色");
            comboBox3.Items.Add("蓝色");
            comboBox3.SelectedIndex = 0;
            //多个控件的同一种事件，共用一个方法，使代码结构较为简单
            radioButton1.CheckedChanged +=
            new EventHandler(AllradioButton_CheckedChanged);
                        radioButton2.CheckedChanged +=
            new EventHandler(AllradioButton_CheckedChanged);
                        radioButton3.CheckedChanged +=
            new EventHandler(AllradioButton_CheckedChanged);
                        radioButton4.CheckedChanged +=
            new EventHandler(AllradioButton_CheckedChanged);
                        radioButton5.CheckedChanged +=
            new EventHandler(AllradioButton_CheckedChanged);
                        radioButton6.CheckedChanged +=
            new EventHandler(AllradioButton_CheckedChanged);
                        radioButton7.CheckedChanged +=
            new EventHandler(AllradioButton_CheckedChanged);

            comboBox1.SelectedIndexChanged +=
            new EventHandler(AllcomboBox_SelectedIndexChanged);
                        comboBox2.SelectedIndexChanged +=
            new EventHandler(AllcomboBox_SelectedIndexChanged);
                        comboBox3.SelectedIndexChanged +=
            new EventHandler(AllcomboBox_SelectedIndexChanged);
        }
        private void AllradioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked) cc.flag = 0; //不绘图
            if (radioButton2.Checked) cc.flag = 1; //直线
            if (radioButton3.Checked) cc.flag = 2; //椭圆
            if (radioButton4.Checked) cc.flag = 3; //矩形
            if (radioButton5.Checked) cc.flag = 4; //铅笔
            if (radioButton6.Checked) cc.flag = 5; //铅笔
            if (radioButton7.Checked) cc.flag = 6; //铅笔

        }
        private void AllcomboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            if (cb.Name == "comboBox1")//边框宽度
            {
                if (cb.SelectedIndex == 0) cc.frameWidth = 1;
                if (cb.SelectedIndex == 1) cc.frameWidth = 5;
                if (cb.SelectedIndex == 2) cc.frameWidth = 10;
            }
            if (cb.Name == "comboBox2")//边框颜色
            {
                if (cb.SelectedIndex == 0) cc.FrameColor = Color.Red;
                if (cb.SelectedIndex == 1) cc.FrameColor = Color.Green;
                if (cb.SelectedIndex == 2) cc.FrameColor = Color.Blue;
            }
            if (cb.Name == "comboBox3")//填充颜色
            {
                if (cb.SelectedIndex == 0) cc.FillColor = Color.Red;
                if (cb.SelectedIndex == 1) cc.FillColor = Color.Green;
                if (cb.SelectedIndex == 2) cc.FillColor = Color.Blue;
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            cc.originPoint = e.Location;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) //按住鼠标左键绘制
            {
                cc.endPoint = e.Location;
                //铅笔（轨迹线）功能不需在鼠标抬起时确定最终绘图，
                //所以直接在pictureBox.IMage上画。
                if (cc.flag == 4)
                {
                    imc.graphics.DrawLine(new Pen(cc.FrameColor, cc.frameWidth),
 cc.originPoint, cc.endPoint);
                    cc.originPoint = cc.endPoint;
                }
                pictureBox1.Refresh();//强制刷新
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (cc.flag == 1)//直线
                e.Graphics.DrawLine(new Pen(cc.FrameColor, cc.frameWidth),
cc.originPoint, cc.endPoint);
            if (cc.flag == 2)//椭圆
            {
                Size rcSize = new Size(cc.endPoint.X - cc.originPoint.X,
                        cc.endPoint.Y - cc.originPoint.Y);
                Rectangle rc = new Rectangle(cc.originPoint, rcSize);
                e.Graphics.FillEllipse(new SolidBrush(cc.FillColor), rc);
                e.Graphics.DrawEllipse(new Pen(cc.FrameColor, cc.frameWidth), rc);
            }
            if (cc.flag == 3)//矩形
            {
                Size rcSize = new Size(cc.endPoint.X - cc.originPoint.X,
                        cc.endPoint.Y - cc.originPoint.Y);
                Rectangle rc = new Rectangle(cc.originPoint, rcSize);
                
                e.Graphics.FillRectangle(new SolidBrush(cc.FillColor), rc);
                
                e.Graphics.DrawRectangle(new Pen(cc.FrameColor, cc.frameWidth), rc);
            }
            if(cc.flag == 5)//三角形
            {
                e.Graphics.FillPolygon(new SolidBrush(cc.FillColor), new PointF[]{
                        new Point(cc.originPoint.X,cc.originPoint.Y),
                        new Point(cc.endPoint.X/2,cc.endPoint.Y),
                        new Point(cc.endPoint.X,cc.endPoint.Y)
                    });
                e.Graphics.DrawPolygon(new Pen(cc.FrameColor, cc.frameWidth),
                    new PointF[]{
                        new Point(cc.originPoint.X,cc.originPoint.Y),
                        new Point(cc.endPoint.X/2,cc.endPoint.Y),
                        new Point(cc.endPoint.X,cc.endPoint.Y)
                    });
            }
            if (cc.flag == 6)
            {
                Size rcSize = new Size(cc.endPoint.X - cc.originPoint.X,
                        cc.endPoint.Y - cc.originPoint.Y);
                Rectangle rc = new Rectangle(cc.originPoint, rcSize);

                e.Graphics.DrawBeziers(
                    new Pen(cc.FrameColor, cc.frameWidth),
                    new PointF[]{
                        new Point(cc.originPoint.X,cc.originPoint.Y),
                        new Point(cc.endPoint.X/2,cc.endPoint.Y),
                        new Point(cc.endPoint.X,cc.endPoint.Y/2),
                        new Point(cc.endPoint.X,cc.endPoint.Y)
                    });
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (cc.flag == 1)
            {
                imc.graphics.DrawLine(new Pen(cc.FrameColor, cc.frameWidth),
 cc.originPoint, cc.endPoint);
            }
            if (cc.flag == 2)
            {
                Size rcSize = new Size(cc.endPoint.X - cc.originPoint.X,
cc.endPoint.Y - cc.originPoint.Y);
                Rectangle rc = new Rectangle(cc.originPoint, rcSize);
                imc.graphics.FillEllipse(new SolidBrush(cc.FillColor), rc);
                imc.graphics.DrawEllipse(new Pen(cc.FrameColor, cc.frameWidth), rc);
            }
            if (cc.flag == 3)
            {
                Size rcSize = new Size(cc.endPoint.X - cc.originPoint.X,
cc.endPoint.Y - cc.originPoint.Y);
                Rectangle rc = new Rectangle(cc.originPoint, rcSize);
                imc.graphics.FillRectangle(new SolidBrush(cc.FillColor), rc);
                imc.graphics.DrawRectangle(new Pen(cc.FrameColor, cc.frameWidth), rc);
            }if(cc.flag == 5)
            {
                Size rcSize = new Size(cc.endPoint.X - cc.originPoint.X,
cc.endPoint.Y - cc.originPoint.Y);
                Rectangle rc = new Rectangle(cc.originPoint, rcSize);
                imc.graphics.FillPolygon(new SolidBrush(cc.FillColor), new PointF[]{
                        new Point(cc.originPoint.X,cc.originPoint.Y),
                        new Point(cc.endPoint.X/2,cc.endPoint.Y),
                        new Point(cc.endPoint.X,cc.endPoint.Y)
                    });
                imc.graphics.DrawPolygon(new Pen(cc.FrameColor, cc.frameWidth),
                    new PointF[]{
                        new Point(cc.originPoint.X,cc.originPoint.Y),
                        new Point(cc.endPoint.X/2,cc.endPoint.Y),
                        new Point(cc.endPoint.X,cc.endPoint.Y)
                    });
            }
            if (cc.flag == 6)
            {
                Size rcSize = new Size(cc.endPoint.X - cc.originPoint.X,
                        cc.endPoint.Y - cc.originPoint.Y);
                Rectangle rc = new Rectangle(cc.originPoint, rcSize);

                imc.graphics.DrawBeziers(
                    new Pen(cc.FrameColor, cc.frameWidth),
                    new PointF[]{
                        new Point(cc.originPoint.X,cc.originPoint.Y),
                        new Point(cc.endPoint.X/2,cc.endPoint.Y),
                        new Point(cc.endPoint.X,cc.endPoint.Y/2),
                        new Point(cc.endPoint.X,cc.endPoint.Y)
                    });
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "JPEG文件|*.jpg|位图文件|*.bmp|Png文件|*.png";
            if (DialogResult.OK == of.ShowDialog())
            {
                Bitmap bt = new Bitmap(of.FileName);
                imc.graphics.DrawImage(bt, new Point(0, 0));
                pictureBox1.Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter = "JPEG文件|*.jpg|位图文件|*.bmp|Png文件|*.png";
            if (DialogResult.OK == sf.ShowDialog())
            {
                imc.SaveToFile(sf.FileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
